package com.castle.fortress.admin.job.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.castle.fortress.admin.oa.entity.HolidayTypeEntity;
import com.castle.fortress.admin.oa.service.HolidayTypeService;
import com.castle.fortress.admin.oa.service.UserHolidaySettingService;
import com.castle.fortress.admin.system.service.ApiSecretService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 释放用户假期余额
 * 每天凌晨1点执行
 **/
@Component("releaseUserHolidayBalanceTask")
public class ReleaseUserHolidayBalanceTask implements ITask {
    @Autowired

    private HolidayTypeService holidayTypeService;

    @Autowired
    private UserHolidaySettingService userHolidaySettingService;

    @Override
    public void runTask(String params) {
        final DateTime now = DateTime.now();
        final int month = now.getField(DateField.MONTH) + 1;
        final int day = now.getField(DateField.DAY_OF_MONTH);
        final List<HolidayTypeEntity> list = holidayTypeService.list();
        list.forEach(type -> {
            Integer giveDay = type.getGiveDay();
            Integer giveMonth = type.getGiveMonth();
            if (giveDay.equals(day) && giveMonth.equals(month)) {
                System.out.println(type.getName() + "需要执行");
                userHolidaySettingService.asyncUpdateUserSetting(type);
            }
        });
    }
}
